home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / prsr_lib / attribut.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  10.0 KB  |  316 lines

  1. /*****************************************************************************
  2. * Setting attributes for geometric objects.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.2, Mar. 1990   *
  5. *****************************************************************************/
  6.  
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include "irit_sm.h"
  11. #include "iritprsr.h"
  12. #include "attribut.h"
  13. #include "allocate.h"
  14.  
  15. /*****************************************************************************
  16. *   Routine to set the color of an object.                     *
  17. *****************************************************************************/
  18. void AttrSetObjectColor(IPObjectStruct *PObj, int Color)
  19. {
  20.     AttrSetObjectIntAttrib(PObj, "color", Color);
  21. }
  22.  
  23. /*****************************************************************************
  24. *   Routine to return the color of an object.                     *
  25. *****************************************************************************/
  26. int AttrGetObjectColor(IPObjectStruct *PObj)
  27. {
  28.     IPAttributeStruct
  29.     *Attr = AttrFindAttribute(PObj -> Attrs, "color");
  30.  
  31.     if (Attr != NULL) {
  32.     if (Attr -> Type == IP_ATTR_INT)
  33.         return Attr -> U.I;
  34.     else if (Attr -> Type == IP_ATTR_STR)
  35.         return atoi(Attr -> U.Str);
  36.     else
  37.         return IP_ATTR_NO_COLOR;
  38.     }
  39.     else
  40.         return IP_ATTR_NO_COLOR;
  41. }
  42.  
  43. /*****************************************************************************
  44. *   Routine to set the RGB color of an object.                     *
  45. *****************************************************************************/
  46. void AttrSetObjectRGBColor(IPObjectStruct *PObj, int Red, int Green, int Blue)
  47. {
  48.     char SRGB[30];
  49.  
  50.     sprintf(SRGB, "%d,%d,%d", Red, Green, Blue);
  51.  
  52.     AttrSetObjectStrAttrib(PObj, "rgb", SRGB);
  53. }
  54.  
  55. /*****************************************************************************
  56. *   Routine to return the RGB color of an object.                 *
  57. *****************************************************************************/
  58. int AttrGetObjectRGBColor(IPObjectStruct *PObj,
  59.               int *Red, int *Green, int *Blue)
  60. {
  61.     char
  62.     *p = AttrGetStrAttrib(PObj -> Attrs, "rgb");
  63.  
  64.     return p != NULL && sscanf(p, "%d,%d,%d", Red, Green, Blue) == 3;
  65. }
  66.  
  67. /*****************************************************************************
  68. *   Routine to set an integer attribute to an object.                 *
  69. *****************************************************************************/
  70. void AttrSetObjectIntAttrib(IPObjectStruct *PObj, char *Name, int Data)
  71. {
  72.     int i;
  73.     IPObjectStruct *PObjTmp;
  74.  
  75.     if (IP_IS_OLST_OBJ(PObj)) {
  76.     for (i = 0; (PObjTmp = ListObjectGet(PObj, i)) != NULL; i++)
  77.         AttrSetObjectIntAttrib(PObjTmp, Name, Data);
  78.     }
  79.     else {
  80.     AttrSetIntAttrib(&PObj -> Attrs, Name, Data);
  81.     }
  82. }
  83.  
  84. /*****************************************************************************
  85. *   Routine to get an integer attribute from an object.                 *
  86. *****************************************************************************/
  87. int AttrGetObjectIntAttrib(IPObjectStruct *PObj, char *Name)
  88. {
  89.     return AttrGetIntAttrib(PObj -> Attrs, Name);
  90. }
  91.  
  92. /*****************************************************************************
  93. *   Routine to set a pointer attribute to an object.                 *
  94. *****************************************************************************/
  95. void AttrSetObjectPtrAttrib(IPObjectStruct *PObj, char *Name, VoidPtr Data)
  96. {
  97.     int i;
  98.     IPObjectStruct *PObjTmp;
  99.  
  100.     if (IP_IS_OLST_OBJ(PObj)) {
  101.     for (i = 0; (PObjTmp = ListObjectGet(PObj, i)) != NULL; i++)
  102.         AttrSetObjectPtrAttrib(PObjTmp, Name, Data);
  103.     }
  104.     else {
  105.     AttrSetPtrAttrib(&PObj -> Attrs, Name, Data);
  106.     }
  107. }
  108.  
  109. /*****************************************************************************
  110. *   Routine to get a pointer attribute from an object.                 *
  111. *****************************************************************************/
  112. VoidPtr AttrGetObjectPtrAttrib(IPObjectStruct *PObj, char *Name)
  113. {
  114.     return AttrGetPtrAttrib(PObj -> Attrs, Name);
  115. }
  116.  
  117. /*****************************************************************************
  118. *   Routine to set a real attribute to an object.                 *
  119. *****************************************************************************/
  120. void AttrSetObjectRealAttrib(IPObjectStruct *PObj, char *Name, RealType Data)
  121. {
  122.     int i;
  123.     IPObjectStruct *PObjTmp;
  124.  
  125.     if (IP_IS_OLST_OBJ(PObj)) {
  126.     for (i = 0; (PObjTmp = ListObjectGet(PObj, i)) != NULL; i++)
  127.         AttrSetObjectRealAttrib(PObjTmp, Name, Data);
  128.     }
  129.     else {
  130.     AttrSetRealAttrib(&PObj -> Attrs, Name, Data);
  131.     }
  132. }
  133.  
  134. /*****************************************************************************
  135. *   Routine to get a real attribute from an object.                 *
  136. *****************************************************************************/
  137. RealType AttrGetObjectRealAttrib(IPObjectStruct *PObj, char *Name)
  138. {
  139.     return AttrGetRealAttrib(PObj -> Attrs, Name);
  140. }
  141.  
  142. /*****************************************************************************
  143. *   Routine to set a string attribute to an object.                 *
  144. *****************************************************************************/
  145. void AttrSetObjectStrAttrib(IPObjectStruct *PObj, char *Name, char *Data)
  146. {
  147.     int i;
  148.     IPObjectStruct *PObjTmp;
  149.  
  150.     if (IP_IS_OLST_OBJ(PObj)) {
  151.     for (i = 0; (PObjTmp = ListObjectGet(PObj, i)) != NULL; i++)
  152.         AttrSetObjectStrAttrib(PObjTmp, Name, Data);
  153.     }
  154.     else {
  155.     AttrSetStrAttrib(&PObj -> Attrs, Name, Data);
  156.     }
  157. }
  158.  
  159. /*****************************************************************************
  160. *   Routine to get a string attribute from an object.                 *
  161. *****************************************************************************/
  162. char *AttrGetObjectStrAttrib(IPObjectStruct *PObj, char *Name)
  163. {
  164.     return AttrGetStrAttrib(PObj -> Attrs, Name);
  165. }
  166.  
  167. /*****************************************************************************
  168. *   Routine to set an object attribute to an object.                 *
  169. *****************************************************************************/
  170. void AttrSetObjectObjAttrib(IPObjectStruct *PObj, char *Name,
  171.                             IPObjectStruct *Data)
  172. {
  173.     int i;
  174.     IPObjectStruct *PObjTmp;
  175.  
  176.     if (IP_IS_OLST_OBJ(PObj)) {
  177.     for (i = 0; (PObjTmp = ListObjectGet(PObj, i)) != NULL; i++)
  178.         AttrSetObjectObjAttrib(PObjTmp, Name, Data);
  179.     }
  180.     else {
  181.     AttrSetObjAttrib(&PObj -> Attrs, Name, Data);
  182.     }
  183. }
  184.  
  185. /*****************************************************************************
  186. *   Routine to set an object attribute.                         *
  187. *****************************************************************************/
  188. void AttrSetObjAttrib(IPAttributeStruct **Attrs, char *Name,
  189.                             IPObjectStruct *Data)
  190. {
  191.     IPAttributeStruct
  192.         *Attr = AttrFindAttribute(*Attrs, Name);
  193.  
  194.     if (Attr) {
  195.     _AttrFreeAttributeData(Attr);
  196.     Attr -> U.PObj = Data;
  197.     Attr -> Type = IP_ATTR_OBJ;
  198.     }
  199.     else {
  200.     Attr = _AttrMallocAttribute(Name, IP_ATTR_OBJ);
  201.     Attr -> U.PObj = Data;
  202.     Attr -> Pnext = *Attrs;
  203.     *Attrs = Attr;
  204.     }
  205. }
  206.  
  207. /*****************************************************************************
  208. *   Routine to get an object attribute from an object.                 *
  209. *****************************************************************************/
  210. IPObjectStruct *AttrGetObjectObjAttrib(IPObjectStruct *PObj, char *Name)
  211. {
  212.     return AttrGetObjAttrib(PObj -> Attrs, Name);
  213. }
  214.  
  215. /*****************************************************************************
  216. *   Routine to get an object attribute.                         *
  217. *****************************************************************************/
  218. IPObjectStruct *AttrGetObjAttrib(IPAttributeStruct *Attrs, char *Name)
  219. {
  220.     IPAttributeStruct
  221.     *Attr = AttrFindAttribute(Attrs, Name);
  222.  
  223.     if (Attr != NULL && Attr -> Type == IP_ATTR_OBJ)
  224.         return Attr -> U.PObj;
  225.     else
  226.         return NULL;
  227. }
  228.  
  229. /*****************************************************************************
  230. *   Routine to release the data slot of an attribute.                 *
  231. * This routine also exists in miscatt2.c without object handling. It will be *
  232. * pulled iff no object handling is used (i.e. this file is not linked in).   *
  233. *****************************************************************************/
  234. void _AttrFreeAttributeData(IPAttributeStruct *Attr)
  235. {
  236.     switch (Attr -> Type) {
  237.     case IP_ATTR_INT:
  238.         break;
  239.     case IP_ATTR_REAL:
  240.         break;
  241.     case IP_ATTR_STR:
  242.         IritFree((VoidPtr) Attr -> U.Str);
  243.         break;
  244.     case IP_ATTR_PTR:
  245.         IritFree((VoidPtr) Attr -> U.Ptr);
  246.         break;
  247.     case IP_ATTR_OBJ:
  248.         IPFreeObject(Attr -> U.PObj);
  249.         break;
  250.     default:
  251.         IritPrsrFatalError("Undefined attribute type");
  252.         break;
  253.     }
  254. }
  255.  
  256. /*****************************************************************************
  257. *   Routine to copy an attribute list.                         *
  258. *****************************************************************************/
  259. IPAttributeStruct *AttrCopyAttributes(IPAttributeStruct *Src)
  260. {
  261.     IPAttributeStruct *DestAttr,
  262.     *Dest = NULL;
  263.  
  264.     for ( ; Src != NULL; Src = Src -> Pnext) {
  265.     if (Src -> Name[0] == '_')       /* Do not copy internal attributes. */
  266.         continue;
  267.  
  268.     if (Dest == NULL) {    
  269.         Dest = DestAttr = AttrCopyOneAttribute(Src);
  270.     }
  271.     else {
  272.         DestAttr -> Pnext = AttrCopyOneAttribute(Src);
  273.  
  274.         DestAttr = DestAttr -> Pnext;
  275.     }
  276.     }
  277.  
  278.     return Dest;
  279. }
  280.  
  281. /*****************************************************************************
  282. *   Routine to copy one attribute item.                         *
  283. *****************************************************************************/
  284. IPAttributeStruct *AttrCopyOneAttribute(IPAttributeStruct *Src)
  285. {
  286.     IPAttributeStruct *Dest;
  287.  
  288.     if (Src -> Name[0] == '_')           /* Do not copy internal attributes. */
  289.     return NULL;
  290.  
  291.     Dest = _AttrMallocAttribute(Src -> Name, Src -> Type);
  292.  
  293.     switch (Src -> Type) {
  294.         case IP_ATTR_INT:
  295.         Dest -> U.I = Src -> U.I;
  296.         break;
  297.     case IP_ATTR_REAL:
  298.         Dest -> U.R = Src -> U.R;
  299.         break;
  300.     case IP_ATTR_STR:
  301.         Dest -> U.Str = IritStrdup(Src -> U.Str);
  302.         break;
  303.     case IP_ATTR_OBJ:
  304.         Dest -> U.PObj = CopyObject(NULL, Src -> U.PObj, TRUE);
  305.         break;
  306.     case IP_ATTR_PTR:
  307.         IritPrsrFatalError("Attempt to copy a pointer attribute");
  308.         break;
  309.     default:
  310.         IritPrsrFatalError("Undefined attribute type");
  311.         break;
  312.     }
  313.  
  314.     return Dest;
  315. }
  316.